import { Component, OnInit, ViewChild } from "@angular/core";
import { DeviceEditComponent } from "../../device/device-list/device-edit/device-edit.component";
import { NzMessageService } from "ng-zorro-antd/message";
import { TaskService } from "./task.service";

@Component({
  selector: "app-task-manager",
  templateUrl: "./task-manager.component.html",
  styles: []
})
export class TaskManagerComponent implements OnInit {


  loading = false;

  // 设备组数据
  deviceGroups = [];

  // 分页参数
  pageData = { page: 0, size: 10 };
  // 查询条件
  searchBeans = [{
    attributeName: "code",
    searchType: "LIKE",
    attributeValue: ""
  }, {
    attributeName: "name",
    searchType: "LIKE",
    attributeValue: ""
  }];

  @ViewChild("deviceEditComponent", { static: false })
  public deviceEditComponent: DeviceEditComponent;

  constructor(private taskService: TaskService,
              private msgSrv: NzMessageService) {
  }

  ngOnInit(): void {
    this.loadListData();
  }

  delete(name, group) {
    this.loading = true;
    this.taskService.deleteJob(name, group).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.msgSrv.success("删除完成");
        this.loadListData();
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success("已取消");
  }

  openEdit(record ?: any): void {

    console.log(record);
    this.deviceEditComponent.edit(record);

  }

  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = "";
    });
  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.taskService.getAll().subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.deviceGroups = res.data;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  _onReuseInit() {
    this.loadListData();
  }

}
